package com.android.mapexample;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.mapexample.utilities.Utility;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity
{
	String TAG = "MainActivity";
	private GoogleMap gMap ;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		try
		{
			if(Utility.isOnline(MainActivity.this))
			{
				initializeMap();
			}
			else
			{
				showToast("Network not available.");
			}

		}
		catch(Exception e)
		{
			Log.i(TAG, "Exception while creating maps.");
			e.printStackTrace();
		}

	}

	@Override
	protected void onResume()
	{
		super.onResume();
		try
		{
			initializeMap();
		}
		catch(Exception e)
		{
			Log.i(TAG, "Exception while creating maps.");
			e.printStackTrace();
		}

	}

	/**
	 * loads Map, if map is not created, it creates for you.
	 */
	private void initializeMap()
	{
		if(gMap == null)
		{
			gMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.mapFrag)).getMap();

			if(gMap == null)
			{
				showToast("Sorry! Unable to create Maps.");
			}
		}

	}

	public void showToast(String msg)
	{
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	}

}
